package behaviorPattern.observer.jdk9;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * @author : Alex Hu
 * date : 2020/3/22 上午09:00
 * description :
 */

public class Java9DemoObserver {

    private String demoName;
    private PropertyChangeSupport listeners = new PropertyChangeSupport(this);

    public Java9DemoObserver(String demoName) {
        this.demoName = demoName;
    }


    public String getDemoName() {
        return this.demoName;
    }

    public void setDemoName(String demoName) {
        String oldValue = this.demoName;
        this.demoName = demoName;
        //发布监听事件
        firePropertyChange("demoName", oldValue, demoName);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listeners.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        listeners.removePropertyChangeListener(listener);
    }

    public void firePropertyChange(String prop, Object oldValue, Object newValue) {
        listeners.firePropertyChange(prop, oldValue, newValue);
    }
}

